﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
using Beway.ValueAdded.Model;
using Beway.ValueAdded.Service;

namespace Beway.ValueAdded.Admin.Controllers
{
    public class ManagerController : BaseController
    {
        /// <summary>
        /// 商户服务类
        /// </summary>               
        public ReUserServ ServMer = SingletonProvider<ReUserServ>.Instance;

        /// <summary>
        /// 管理员类
        /// </summary>
        public AdminUserServ Service = SingletonProvider<AdminUserServ>.Instance;
        // GET: /Manager/

        public ActionResult List()
        {

            return View();
        }



        /// <summary>
        /// 所有项目经理信息
        /// </summary>
        /// <returns></returns>
        public ActionResult MerList(string username = "")
        {
            var q = Service.GetAllMerList().Select(m => new {
                AdminId = m.AdminID,
                TrueName = m.TrueName,
                AdminUserName = m.AdminUserName,
            });

            if (!string.IsNullOrEmpty(username))
            {
                q = q.Where(m => m.AdminUserName.Contains(username));
                int id = 0;
                int.TryParse(username, out id);
                if (id > 0)
                {
                    q = q.Where(m => m.AdminId == id);
                }
            }

            return Json(q);
        }


        public JsonResult ManagerList(int? page, int? rows, int? merchantid, string UserName)
        {
            int ListCount = 0;
            object d = null;
            if (CurrentRole == ManagerRoleType.SystemAdmin)
            {
                d = Service.AdminUserList(page ?? 0, rows ?? 0, UserName, out ListCount);
            }

            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", ListCount);
            json.Add("rows", d);
            return Json(json, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public ActionResult AddManager(AdminUser Model)
        {
            if (CurrentRole == ManagerRoleType.SystemAdmin)
            {
                int Result = Service.AddAdminUser(Model);
                return Content(Result.ToString());
            }
            else
            {
                return Content("0");
            }

        }
        [HttpPost]
        public ActionResult UpdateManager(AdminUser Model)
        {
            if (CurrentRole == ManagerRoleType.SystemAdmin)
            {
                int Result = Service.UpdateAdminUser(Model);
                return Content(Result.ToString());
            }
            else
            {
                return Content("0");
            }
        }
        [HttpPost]
        public ActionResult DelManager(int id)
        {
            if (CurrentRole == ManagerRoleType.SystemAdmin)
            {
                int isdel = Service.DelAdminUser(id);
                string ret = "";
                if (isdel > 0)
                {
                    ret = "ok";
                }
                else
                {
                    ret = "删除失败";
                }
                return Content(ret);
            }
            else
            {
                return Content("删除失败");
            }
        }

    }
}
